qt制作软键盘(虚拟键盘) |
您所在的位置:网站首页 › qt 虚拟键盘 自定义 › qt制作软键盘(虚拟键盘) |
步骤
1.制作界面
2、重写eventFilter事件,判断当前触发对象是否是添加了过滤器的控件,且事件是否是鼠标按钮点击事件。是的话,显示软键盘,并将焦点设置到当前控件上 bool softkey::eventFilter(QObject *watched, QEvent *event) { if ( (watched == ui->userEdit) && (event->type() == QEvent::MouseButtonPress) ) { ui->myWidget->show(); ui->userEdit->setFocus(); } else if ( (watched == ui->passwordEdit) && (event->type() == QEvent::MouseButtonPress) ) { ui->myWidget->show(); ui->passwordEdit->setFocus(); } return QMainWindow::eventFilter(watched,event); } 3.若是当前控件编辑完毕则隐藏软键盘重写类mylabel自定义信号close_key,当判断当前事件是鼠标点击,则发散close_key信号,并且返回true,Qt 会认为这个事件已经处理完毕,不会再将这个事件发送给其它对象。 class mylabel : public QLabel { Q_OBJECT public: explicit mylabel(QWidget *parent = Q_NULLPTR); bool event(QEvent *e) override { if(e->type() == QEvent :: MouseButtonPress){ emit close_key(); return true; } return QLabel::event(e); } signals: void close_key(); }; connect(ui->backgroundlabel,&mylabel::close_key,this,&softkey::hide_widget); void softkey::hide_widget() { ui->myWidget->hide(); } 4、在自定义按钮类softKey的槽函数中判断当前点击的按钮文本,定义一个按键事件,通过postEvent将事件key发送出去 softkeys::softkeys(QWidget *parent) : QToolButton(parent) { connect(this,SIGNAL(clicked(bool)),this,SLOT(button_clicked(bool))); } void softkeys::button_clicked(bool) { //this->text() ----获得控件上的文本内容,比如,删除 if(this->text() == " |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |